﻿using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AlfarEngine.Utilities.ShadowMapping;

namespace AlfarEngine.Components
{
    public class SpotLight : Light
    {
        #region Variables

        protected Vector2 mDirection;
        protected float mAngle;

        static protected Texture2D sMask = null;
        static protected double Sqrt2 = Math.Sqrt(2.0);

        #endregion

        //-------------------------------------------

        #region Accessors

        public Vector2 direction { get { return mDirection; } set { mDirection = value; if (mDirection.LengthSquared() > 1) mDirection.Normalize(); } }
        public float angle { get { return mAngle; } set { mAngle = value; } }

        #endregion

        //----------------------------------------------

        #region Methods

        public SpotLight(GameObject pOwner)
            : base(pOwner)
        {
            if (sMask == null)
            {
                sMask = new Texture2D(Engine.game.GraphicsDevice, 1, 1);
                sMask.SetData(new Color[] { new Color(1.0f, 1.0f, 1.0f, 1.0f) });
            }

            mDirection = -Vector2.UnitY;
            mAngle = 20.0f;

            setRange(200.0f);
        }

        protected override void setRange(float pRange)
        {
            base.setRange(pRange);

            mResolver = new ShadowmapResolverSpot(Engine.game.GraphicsDevice, sQuad, ShadowmapSize.Size256, ShadowmapSize.Size1024);
            mResolver.LoadContent();

            ((ShadowmapResolverSpot)mResolver).Spot = this;
        }

        #endregion
    }
}
